/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ow.framework.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import jxl.Cell;
import jxl.Workbook;

/**
 * 
 */
public class FileHelper {

	/**
	 * 创建文件夹
	 * 
	 * @param filePath
	 *            文件夹路径
	 * @throws IOException
	 */
	public static void createFolder(String filePath) {
		File file = new File(filePath);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

	/**
	 * 判断文件类型的类，
	 * 
	 * @param filePath
	 * @return 1 Text文本类型
	 * @return 2 Excel表格类型
	 */
	public static int judgeFileType(String filePath) {
		if (filePath.endsWith("text") || filePath.endsWith("txt")) {
			return 1;
		}
		if (filePath.endsWith("xls")) {
			return 2;
		}
		return 0;
	}

	/**
	 * 返回映射文件.config的内容 fieldContent 为对应的字段内容 fieldDescription 为对应的字段的描述信息
	 * 
	 * @param mappingFile
	 * @return
	 */
	public static List<HashMap<String, String>> getMappingFile(
			String mappingFile) {
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(mappingFile), "UTF-8"));
			String[] fileContent = null;
			List<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
			String str = br.readLine();
			while (str != null) {
				fileContent = str.split(":");
				HashMap<String, String> hm = new HashMap<String, String>();
				if (fileContent.length >= 1) {
					hm.put("fieldContent", fileContent[0]);
					hm.put("fieldDescription", fileContent[1]);
					arrayList.add(hm);
					str = br.readLine();
				} else {
					continue;
				}
			}
			return arrayList;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 得到文件首行的字段内容，相对模块客户导入与积分导入模块 传回的List为封装多个HashMap。
	 * HashMap中contentPosition字段封装位置； HashMap中contentLine字段封装该位置对应的位置；
	 */
	public static List<HashMap<String, String>> getFileLineContent(
			String uploadFile) {
		if (judgeFileType(uploadFile) == 1) {
			try {
				BufferedReader br = new BufferedReader(new InputStreamReader(
						new FileInputStream(uploadFile), "GB2312"));
				List<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
				String[] strFirLine = br.readLine().split(",");
				for (int j = 0; j < strFirLine.length; j++) {
					HashMap<String, String> contentMap = new HashMap<String, String>();
					contentMap.put("contentPosition", "position_" + j);
					contentMap.put("contentLine", strFirLine[j]);
					arrayList.add(contentMap);
				}
				return arrayList;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		} else if (judgeFileType(uploadFile) == 2) {
			List<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();
			try {
				// 打开上传的表格
				InputStream is = new FileInputStream(uploadFile);
				// 得到工作表里的内容
				jxl.Workbook rwb = Workbook.getWorkbook(is);
				// 读取第一个工作表的数据
				jxl.Sheet rs = rwb.getSheet(0);
				// 这里涉及到内容的回显,以及数据库字段的映射所以这里只取第一行
				int num = 0;
				// 得到第i行的数据..返回cell数组
				Cell[] cell = rs.getRow(num);
				for (int i = 0; i < cell.length; i++) {
					HashMap<String, String> contentMap = new HashMap<String, String>();
					contentMap.put("contentPosition", "position_" + i);
					contentMap.put("contentLine", cell[i].getContents());
					arrayList.add(contentMap);
				}
				return arrayList;
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
		return null;
	}

	/**
	 * 此方法只作为内容的写入，不做任务逻辑操作
	 * 
	 * @param filePath
	 *            写入的文本的路径
	 * @param contentSrc
	 *            需要写入的内容，格式已经在调用该方法前处理好，
	 * @return 成功 or 失败
	 */
	public static boolean writeFile(String filePath, String contentSrc) {
		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(filePath, true);
			if (fileWriter != null) {
				fileWriter.write(contentSrc);
				fileWriter.flush();
				fileWriter.close();
				return true;
			} else {
				return false;
			}
		} catch (IOException e2) {
			if (fileWriter != null) {
				try {
					fileWriter.flush();
					fileWriter.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			System.out.println(e2.getMessage());
			return false;
		}
	}

	/**
	 * 
	 * @param filePath
	 *            存储文件
	 * @param uploadFile
	 *            上传文件
	 * @param fileName
	 *            文件名称
	 * @return
	 */
	public static boolean writeFile(File filePath, File uploadFile) {
		FileOutputStream fos = null;
		FileInputStream fis = null;
		try {

			fos = new FileOutputStream(filePath);
			fis = new FileInputStream(uploadFile);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}

			if (fos != null) {
				fos.close();
			}

			if (fis != null) {
				fos.close();
			}

		} catch (Exception e) {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
					return false;
				}

			}
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e1) {
					e1.printStackTrace();
					return false;
				}

			}
			e.printStackTrace();
			return false;
		}

		return true;
	}

	/**
	 * 
	 * @param path
	 *            文件所在路径
	 * @param fileName
	 *            文件名称
	 * @return
	 */
	public static boolean deleteFile(String path, String fileName) {
		try {
			File f = new File(path + File.separator + fileName);
			if (f.exists()) {
				f.delete();
			}

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}

	/**
	 * 单独传入完整路径删除相关文件信息
	 * 
	 * @param path
	 * @return
	 */
	public static boolean deleteFile(String path) {
		try {
			File f = new File(path);
			if (f.exists()) {
				f.delete();
			}

		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}
}
